package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacGroupIpAddrMapper;
import com.csun.cmny.provider.model.domain.UacGroupIpAddr;
import com.csun.cmny.provider.model.vo.ipauth.IpAddrVo;
import com.csun.cmny.provider.service.UacGroupIpAddrService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-09-30 14:20
 * @Description:
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UacGroupIpAddrServiceImpl extends BaseService<UacGroupIpAddr> implements UacGroupIpAddrService {

    @Resource
    private UacGroupIpAddrMapper uacGroupIpAddrMapper;

    /**
     * select by group id
     *
     * @param groupId the group id
     *
     * @return the list.
     */
    @Override
    public List<IpAddrVo> selectByGroupId(Long groupId) {

        return uacGroupIpAddrMapper.selectByGroupId(groupId);
    }

    /**
     * set ip auth
     *
     * @param ipAddrVoList the ip addr vo list
     * @param loginAuthDto the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer setIpAuth(List<IpAddrVo> ipAddrVoList, LoginAuthDto loginAuthDto) {

        // 根据groupId删除
        Integer result = uacGroupIpAddrMapper.deleteByGroupId(loginAuthDto.getGroupId());

        List<UacGroupIpAddr> uacGroupIpAddrList = Lists.newArrayList();
        // 批量添加
        for (IpAddrVo v : ipAddrVoList) {
            UacGroupIpAddr uacGroupIpAddr = new UacGroupIpAddr();
            uacGroupIpAddr.setUpdateInfo(loginAuthDto);
            uacGroupIpAddr.setGroupId(loginAuthDto.getGroupId());
            uacGroupIpAddr.setIp(v.getIp());
            uacGroupIpAddr.setNumber(v.getNumber());
            uacGroupIpAddr.setRemark(v.getRemark());

            uacGroupIpAddrList.add(uacGroupIpAddr);
        }


        return batchSave(uacGroupIpAddrList);
    }

    /**
     * delete by group id
     *
     * @param groupId the group id
     *
     * @return the int.
     */
    @Override
    public Integer deleteByGroupId(Long groupId) {

        return uacGroupIpAddrMapper.deleteByGroupId(groupId);
    }

    /**
     * save uac group ip addr
     *
     * @param uacGroupIpAddr the uac group ip addr
     * @param loginAuthDto   the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer save(UacGroupIpAddr uacGroupIpAddr, LoginAuthDto loginAuthDto) {

        uacGroupIpAddr.setUpdateInfo(loginAuthDto);
        uacGroupIpAddr.setGroupId(loginAuthDto.getGroupId());

        if (uacGroupIpAddr.isNew()) {

            return uacGroupIpAddrMapper.insertSelective(uacGroupIpAddr);
        }

        return uacGroupIpAddrMapper.updateByPrimaryKeySelective(uacGroupIpAddr);
    }
}
